package com.zzc.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.zzc.domain.User;
import com.zzc.domain.UserInfor;
import com.zzc.repository.UserInforRep;
import com.zzc.repository.UserRep;
import com.zzc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    UserRep userRep;

    @Autowired
    UserInforRep userInforRep;

    @Override
    public String loginAuthentication(String userId,String pwd) {
        User user = userRep.findByUserId(userId);
        if(user==null){
            return "IdError";
        }else {
            if(user.getUserPassword().equals(pwd)){
                return user.getUserName();
            }else{
                return "PwdError";
            }
        }
    }

    @Override
    public String getUserAddress(String id) {
        return JSON.toJSONString(userInforRep.findByUserId(id));
    }

    @Override
    public String addUser(String userName, String userId, String userPassword) {
        User user1 = userRep.findByUserId(userId);
        if(user1==null){
            User user = new User(userName,userId,userPassword);
            userRep.save(user);
            UserInfor userInfor = new UserInfor();
            userInfor.setUserId(userId);
            userInfor.setUserPhoneNumber(userId);
            userInforRep.save(userInfor);
            return "success";
        }else {
            return "手机号已被注册";
        }


    }

    @Override
    public void updateUserInfor(String phone, String address, String name, String id) {
        userInforRep.upDateByUserId(phone,address,name,id);
    }
}
